Google Apps Script 可在多種 Google 產品中建立自訂選單/自定義選單!
像是 Google 文件、試算表、簡報、或表單中等等都可以實現
而選單中的項目可以與 GAS 中的自定義 Function 做連結
範例: 我想要有一個 My tools 的選單, 下面要有一個叫做 hello 的項目
function onOpen() {
  SpreadsheetApp
    .getUi()
    .createMenu('My tools') // 自訂選單名稱
    .addItem('Say Hello', "hello") // 前:選單項目名稱, 後: 連結的 function 名稱, 注意是"字串"
    .addToUi()
}
Note: hello 的函示可先留空, 下方還有進階教學!
這樣就會產生出一個像下圖的自定義選單啦!是不是超簡單!
如果想再多產出個選單項目,就在後面再多加一個 addItem 就行了!
function onOpen() {
  SpreadsheetApp
    .getUi()
    .createMenu('My tools') 
    .addItem('Say Hello', "hello") 
    .addItem('Say Bye', "bye") // 新增一個選單項目
    .addToUi()
}
如果要創造 SubMenu 的話就使用 .addSubMenu()
function onOpen() {
  SpreadsheetApp
    .getUi()
    .createMenu('My tools') // 自訂選單名稱
    .addItem('Say Hello', "hello") // 前:選單項目名稱, 後: 連結的 function 名稱, 注意是"字串"
    .addSeparator()
    .addSubMenu(ui.createMenu('Sub-menu')
          .addItem('Say goodbye', 'goodbye'))
    .addToUi()
}

假如我今天想要按下按鈕後,他跳出來一個訊息框跟使用者 Say Hello,可以用 getUi().alert 來完成
function onOpen() {
  SpreadsheetApp
    .getUi()
    .createMenu('My tools') // 自訂選單名稱
    .addItem('Say Hello', "hello") // 連結的 function 名稱, 注意是"字串"
    .addToUi()
}
function hello() {
  SpreadsheetApp.getUi() // Or DocumentApp, SlidesApp or FormApp.
     .alert('hello!');
}

若要讓使用者按「確認」或「取消」按鈕,可以搭配使用 ui.ButtonSet.YES_NO
function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('Custom Menu')
      .addItem('Show alert', 'showAlert')
      .addToUi();
}
function showAlert() {
  var ui = SpreadsheetApp.getUi(); // Same variations.
  var result = ui.alert(
     'Please confirm',
     'Are you sure you want to continue?',
      ui.ButtonSet.YES_NO);
  // Process the user's response.
  if (result == ui.Button.YES) {
    // User clicked "Yes".
    ui.alert('Confirmation received.');
  } else {
    // User clicked "No" or X in the title bar.
    ui.alert('Permission denied.');
  }
}


Note: App Script 需繫結至文件、試算表或表單,OnOpen() 函式才能使用。
如果我想要讓使用者輸入訊息框,能用 Prompt Dialog getUi().prompt()
function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('Custom Menu')
      .addItem('Show prompt', 'showPrompt')
      .addToUi();
}
function showPrompt() {
  var ui = SpreadsheetApp.getUi(); // Same variations.
  var result = ui.prompt(
      'Let\'s get to know each other!',
      'Please enter your name:',
      ui.ButtonSet.OK_CANCEL);
  // Process the user's response.
  var button = result.getSelectedButton();
  var name = result.getResponseText();
  if (button == ui.Button.OK) {
    // User clicked "OK".
    ui.alert('Hello ' + name + '.');
  } else if (button == ui.Button.CANCEL) {
    // User clicked "Cancel".
    ui.alert('I didn\'t get your name.');
  } else if (button == ui.Button.CLOSE) {
    // User clicked X in the title bar.
    ui.alert('You closed the dialog.');
  }
}


今天這樣就介紹完了如何自訂選單&跳出訊息框,前往下一天前進吧!
https://spreadsheet.dev/pop-up-alert-messages-in-google-sheets